/*
 ============================================================================
 Name        : push_image.c
 Author      : lzc
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include "axi_lite.h"
#include "share_mem.h"
#include "quantization.h"
#include "test_image.h"
#include "config_ini.h"
#include <stdio.h>          // included for 'printf()'
#include <sys/time.h>       // included for 'gettimeofday()'
#include <time.h>           // included for 'localtime()'



int image_count = 0;
int image_old_count = 0;
int image_frame = 0;
int image_old_frame = 0;
char *pImage_data;
char *pImage_format;
unsigned char *pImage_data_src;


#define SET_AXI_camera_line_number         0x00
#define SET_AXI_whitebalance               0x01

#define SET_AXI_image_rectification        0x02    //图像矫正
#define SET_AXI_camera_fusion_degree       0x03     //图像融合
#define SET_AXI_camera_brightness          0x04    //图像亮度
#define SET_AXI_triggerinput_1             0x05    //触发输入  第一组
#define SET_AXI_triggerinput_2             0x06    //触发输入  第二组
#define SET_AXI_camera_delay               0x07
#define SET_AXI_HDMI                       0x08
#define SET_AXI_marking_setting_h          0x09
#define SET_AXI_marking_setting            0x0A
#define SET_AXI_marking_number             0x0B
#define AXI_W_IMAGE_FINISH                 0x0C
#define GET_FPGA_VERSION                   0x0D
#define AXI_R_DATA_UPDATE                  0x0E
#define AXI_R_IMAGE_SIZE                   0x0F
#define AXI_R_IMAGE_LINE                   0x10
#define AXI_R_IMAGE_FRAME                  0x11



int nCamera_line_number;
int nWhitebalance;
int nCamera_fusion_degree;
int nCamera_brightness;
int nCcamera_delay;
int nCamera_HDMI;
int nTriggerinput_1;
int nTriggerinput_2;
int nMarking_setting;
int nMarking_number;
int nimage_rectification;


int System_cmd(const char *cmd)
{
    pid_t pid;
    if(-1==(pid=vfork()))
    {
         return 1;
    }
    if(0==pid)
    {
        execl("/bin/sh","sh","-c",cmd,(char *)0);
        return  0;
    }
    else
    {
       wait(&pid);
    }
    return 0;
}

void  EndianSwap(unsigned char *pData, int startIndex, int length)
{
    int i,cnt,end,start;
    cnt = length / 2;
    start = startIndex;
    end  = startIndex + length - 1;
    unsigned char tmp;
    for (i = 0; i < cnt; i++)
    {
        tmp            = pData[start+i];
        pData[start+i] = pData[end-i];
        pData[end-i]   = tmp;
    }
}

int main(void)
{
	printf("init\r\n");

	FILE *pfImage_format = NULL;
	FILE *pfImage_data = NULL;
	int i;
	unsigned int image_height = 2160;
	unsigned int image_size = 458332;
	char file_name[256];
	char curl_cmd[1024];

	char local_ip[32] = {0};
        char remote_ip[32] = {0};
        char remote_port[32] = {0};



	pImage_data = (char*) malloc(200*1024*1024);
	pImage_format = (char*) malloc(1024);

	pImage_data_src = (char*) malloc(200*1024*1024);

	pfImage_format = fopen("/opt/ja.jpg","r");
	fread(pImage_format, 1, 1024, pfImage_format);
	fclose(pfImage_format);


	AXI_Lite_Init();
	CShareMem_Init();



	char fisheyeCorrection [32] = {0};
	configFileStrGet("/boa/my.ini","fisheyeCorrection",fisheyeCorrection,32);


	char camera_line_number[32] = {0};
	configFileStrGet("/boa/my.ini","camera_line_number",camera_line_number,32);

	char whitebalance[32] = {0};
	configFileStrGet("/boa/my.ini","whitebalance",whitebalance,32);

	char camera_fusion_degree[32] = {0};
	configFileStrGet("/boa/my.ini","camera_fusion_degree",camera_fusion_degree,32);

	char camera_brightness[32] = {0};
	configFileStrGet("/boa/my.ini","camera_brightness",camera_brightness,32);

	char camera_delay[32] = {0};
	configFileStrGet("/boa/my.ini","camera_delay",camera_delay,32);

	char camera_HDMI_display[32] = {0};
	configFileStrGet("/boa/my.ini","camera_HDMI_display",camera_HDMI_display,32);

	char camera_HDMI_display_switch[32] = {0};
    configFileStrGet("/boa/my.ini","camera_HDMI_display_switch",camera_HDMI_display_switch,32);



	char triggerinput_1[32] = {0};
	configFileStrGet("/boa/my.ini","triggerinput_1",triggerinput_1,32);

	char triggerinput_2[32] = {0};
	configFileStrGet("/boa/my.ini","triggerinput_2",triggerinput_2,32);

	char marking_setting[32] = {0};
	configFileStrGet("/boa/my.ini","marking_setting",marking_setting,32);

	char marking_number[32] = {0};
	configFileStrGet("/boa/my.ini","marking_number",marking_number,32);


	nCamera_line_number = atoi(camera_line_number);
	printf("nCamera_line_number = %d\r\n",nCamera_line_number);

	nCamera_fusion_degree= atoi(camera_fusion_degree);
	printf("camera_fusion_degree = %d\r\n",nCamera_fusion_degree);

	nCamera_brightness= atoi(camera_brightness);
	printf("camera_brightness = %d\r\n",nCamera_brightness);

	nCcamera_delay =  atoi(camera_delay);
	printf("camera_delay = %d\r\n",nCcamera_delay);

	nMarking_number =  atoi(marking_number);
	printf("marking_number = %d\r\n",nMarking_number);



	char *ret =  strstr(fisheyeCorrection,"close");
	if(ret != NULL)
	{
		printf("close = %s\n",fisheyeCorrection);
		nimage_rectification = 0;
	}
	else
	{
		printf("open = %s\n",fisheyeCorrection);
		nimage_rectification = 1;
	}





	ret =  strstr(whitebalance,"close");
	if(ret != NULL)
	{
		printf("close = %s\n",whitebalance);
		nWhitebalance = 0;
	}
	else
	{
		printf("open = %s\n",whitebalance);
		nWhitebalance = 1;
	}

	ret =  strstr(triggerinput_1,"close");
	if(ret != NULL)
	{
		printf("close = %s\n",triggerinput_1);
		nTriggerinput_1 = 0;
	}
	else
	{
		printf("open = %s\n",triggerinput_1);
		nTriggerinput_1 = 1;
	}

	ret =  strstr(triggerinput_2,"close");
	if(ret != NULL)
	{
		printf("close = %s\n",triggerinput_2);
		nTriggerinput_2 = 0;
	}
	else
	{
		printf( "open = %s\n",triggerinput_2);
		nTriggerinput_2 = 1;
	}


	ret =  strstr(marking_setting,"close");
	if(ret != NULL)
	{
		printf("close = %s\n",marking_setting);
		nMarking_setting = 0;
	}
	else
	{
		printf( "open = %s\n",marking_setting);
		nMarking_setting = 1;
	}

	ret =  strstr(camera_HDMI_display,"middle");
	if(ret != NULL)
	{
		printf( "nCamera_HDMI middle\n");
		nCamera_HDMI = 1;
	}

	ret =  strstr(camera_HDMI_display,"left");
	if(ret != NULL)
	{
		printf("nCamera_HDMIleft\n");
		nCamera_HDMI = 2;
	}

	ret =  strstr(camera_HDMI_display,"right");
	if(ret != NULL)
	{
		printf( "nCamera_HDMI right\n");
		nCamera_HDMI = 3;
	}


       ret =  strstr(camera_HDMI_display_switch,"open");
        if(ret != NULL)
        {
                //fprintf(cgiOut, "open = %s\n",marking_setting);
                
		
		 ;
        }
        else
        {
                //fprintf(cgiOut, "close = %s\n",marking_setting);
		 nCamera_HDMI = 0;
                
        }






#if 1

printf("nWhitebalance=%d\r\n",nWhitebalance);

	AXI_Lite_Write(SET_AXI_camera_line_number,nCamera_line_number);


	AXI_Lite_Write(SET_AXI_whitebalance,0);
	usleep(100);
	AXI_Lite_Write(SET_AXI_whitebalance,1);
	usleep(100);
	AXI_Lite_Write(SET_AXI_whitebalance,0);
	usleep(100);



	AXI_Lite_Write(SET_AXI_whitebalance,nWhitebalance);
    AXI_Lite_Write(SET_AXI_image_rectification,nimage_rectification);
	AXI_Lite_Write(SET_AXI_camera_fusion_degree,nCamera_fusion_degree);
	AXI_Lite_Write(SET_AXI_camera_brightness,nCamera_brightness);
	AXI_Lite_Write(SET_AXI_camera_delay,nCcamera_delay);
	AXI_Lite_Write(SET_AXI_triggerinput_1,nTriggerinput_1);
	AXI_Lite_Write(SET_AXI_triggerinput_2,nTriggerinput_2);
	AXI_Lite_Write(SET_AXI_marking_setting_h,nMarking_setting);
	AXI_Lite_Write(SET_AXI_marking_setting,nMarking_setting);
	AXI_Lite_Write(SET_AXI_marking_number,nMarking_number);
	AXI_Lite_Write(SET_AXI_HDMI,nCamera_HDMI);



#endif
//int j =0;
//for(j=0;j<100;j++)
//{
//	AXI_Lite_Write(j,55);

unsigned int fpga_version = AXI_Lite_Read(GET_FPGA_VERSION);
 
printf( "fpga_version = %x\r\n",fpga_version);


char ffpp[32];

sprintf( ffpp,"%x",fpga_version);

configFileStrSet("/boa/my.ini","fpga_version",ffpp);

//}
//
  FILE *file_log;
  file_log = fopen("/home/uisrc/log_count.txt","w");

        

	while(1)
	{ 

		image_count = AXI_Lite_Read(AXI_R_DATA_UPDATE);
                image_frame = AXI_Lite_Read(AXI_R_IMAGE_FRAME);
		if((image_count != image_old_count)||( image_frame != image_old_frame  ))
		{
			image_height = AXI_Lite_Read(AXI_R_IMAGE_LINE);
			image_size = AXI_Lite_Read(AXI_R_IMAGE_SIZE)*4;
//                        image_frame = AXI_Lite_Read(AXI_R_IMAGE_FRAME);

			printf("image_count = %d\r\n",image_count);
			printf("image_height = %d\r\n",image_height);
			printf("image_size = %d\r\n",image_size);
                        printf("image_frame = %d\r\n",image_frame);


			//fprintf(file_log,"count = %d\r\n",image_count);
			//fprintf(file_log,"size = %d\r\n",image_size);
			//fflush(file_log);


			image_old_count = image_count;
			image_old_frame = image_frame;
			 
if(image_size<(200*1024*1024))
{


			struct timeval tv;
			gettimeofday(&tv, NULL);
			time_t sec = tv.tv_sec;
			suseconds_t usec = tv.tv_usec;
			struct tm *lt = localtime(&sec);
	//		int sec_of_day = lt->tm_hour*60*60+lt->tm_min*60;
	//		printf("%d-%d-%d_%d-%d-%d.%ld\n", 1900+lt->tm_year, 1+lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, tv.tv_usec);

			    // 东八区时间偏移，直接在小时上加8
    lt->tm_hour += 8;

    // 通过mktime自动处理超过范围的时间值，比如小时数超过24
    mktime(lt);

			sprintf(file_name,"/mnt/image_%d-%02d-%02d_%02d-%02d-%02d_%d_%d.jpg", 1900+lt->tm_year, 1+lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, image_count, image_frame);
			pfImage_data = fopen(file_name,"wb");

			for(i=0;i<623;i++)
			{
				pImage_data[i] = pImage_format[i];
			}

			for(i=25;i<89;i++)
			{
				pImage_data[i] = image_y_98_80[i-25];
			}

			for(i=94;i<158;i++)
			{
				pImage_data[i] = image_cbcr_98_80[i-94];
			}
			pImage_data[165] = 12;
			pImage_data[166] = 24;
			pImage_data[164] = (image_height & 0xff);
			pImage_data[163] = ((image_height>>8)& 0xff);

			AXI_Lite_Write(AXI_W_IMAGE_FINISH,0);

			CShareMem_Read(0,image_size,pImage_data_src);

		//	for (i = 0; i < image_size; i += 4)
		//	{
		//		EndianSwap(pImage_data_src,i,4);
		//	}
			AXI_Lite_Write(AXI_W_IMAGE_FINISH,1);



		        for(i = 0;i<432;i++)
                        {
                                pImage_data[i+177] = image_head[i];
                        }

			for(i = 0;i<image_size;i++)
			{
				pImage_data[i+623] = pImage_data_src[i];
			}
			pImage_data[623+image_size] = 255;
			pImage_data[623+image_size+1] = 217;
			fwrite(pImage_data, 1, 623+image_size+2, pfImage_data);
fflush(pfImage_data);
			fclose(pfImage_data);

			System_cmd("/bin/sync ");

                        sprintf(curl_cmd,"/bin/rm  /boa/www/jpeg_0_64.jpg -fr ");
                        System_cmd(curl_cmd);
                        System_cmd("/bin/sync ");


			sprintf(curl_cmd,"/bin/cp %s    /boa/www/jpeg_0_64.jpg",file_name);
			fprintf(file_log,"cmd = %s\r\n",curl_cmd);
			System_cmd(curl_cmd);
			System_cmd("/bin/sync ");
}
		    configFileStrGet("/boa/my.ini","local_ip",local_ip,32);
		    configFileStrGet("/boa/my.ini","remote_ip",remote_ip,32);
		    configFileStrGet("/boa/my.ini","remote_port",remote_port,32);
                    usleep(200);
			

			sprintf(curl_cmd,"/usr/bin/curl \"http://%s:%s/newimage?type=info&imgid=13&groupid=0&url=%s\"",remote_ip,remote_port,local_ip);
			//fprintf(cgiOut,"%s\n",curl_cmd);
			System_cmd(curl_cmd);

			sprintf(curl_cmd,"/usr/bin/curl \"http://%s:%s/newimage?type=small&imgid=13&groupid=0&url=%s/jpeg_0_64.jpg&listurl=%s/header_0_64.txt\"",remote_ip,remote_port,local_ip,local_ip);
			//fprintf(cgiOut,"%s\n",curl_cmd);
			System_cmd(curl_cmd);

			sprintf(curl_cmd,"/usr/bin/curl \"http://%s:%s/newimage?type=big&imgid=13&groupid=0&url=%s/jpeg_0_128.jpg&listurl=%s/header_0_128.txt\"",remote_ip,remote_port,local_ip,local_ip);
			//fprintf(cgiOut,"%s\n",curl_cmd);
			System_cmd(curl_cmd);
			
//fprintf(file_log,"ip = %s\r\n",remote_ip);
//fprintf(file_log,"port = %s\r\n",remote_port);


  //                      fflush(file_log);



		}

		usleep(100);
	}


	 AXI_Lite_Close();
	 CShareMem_Close();


	return EXIT_SUCCESS;
}
